#!/usr/bin/env dash
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2024 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
	configure)
		########################################################################
		# Set the default settings of the shairport package to those expected
		# by OpenMediaVault.
		########################################################################
		if [ -z "$2" ]; then
			echo "Updating service units ..."
			deb-systemd-invoke stop shairport-sync || :
			deb-systemd-helper disable shairport-sync || :
		fi

		########################################################################
		# ALSA mixer is muted by default.
		########################################################################
		if [ -z "$2" ]; then
			amixer set Master 100% unmute || :
			amixer set PCM 100% unmute || :
		fi

		########################################################################
		# Activate trigger to rebuild workbench configuration files.
		########################################################################
		dpkg-trigger update-workbench

		########################################################################
		# Initialize and migrate configuration database.
		########################################################################
		echo "Updating configuration database ..."
		omv-confdbadm create "conf.service.shairport"
		if [ -n "$2" ]; then
			omv-confdbadm migrate "conf.service.shairport" "${2}"
		fi

		########################################################################
		# Action to be done depending on the previous version.
		########################################################################
		echo "Updating configuration ..."
		if dpkg --compare-versions "$2" lt-nl "2.4"; then
			rm -f /etc/default/shairport-sync
			omv-mkconf shairport-sync
			if [ "$(omv_config_get "/config/services/shairport/enable")" != "0" ]; then
				invoke-rc.d shairport-sync restart
			fi
		fi
		if dpkg --compare-versions "$2" lt-nl "6.0"; then
			omv_module_set_dirty monit
			omv_module_set_dirty shairport-sync
		fi
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
	;;

	*)
		echo "postinst called with unknown argument '$1'" >&2
		exit 1
	;;
esac

exit 0
